package com.terrynoya.fight.stateMachine.triggers
{
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.emuns.Operator;
	import com.terrynoya.fight.emuns.StateTypeEnum;
	import com.terrynoya.fight.evaluation.Node;
	import com.terrynoya.fight.evaluation.ParseState;

	public class StateType
	{
		public function StateType()
		{
			
		}
		
		public static function eval(state:Object,node:Node):*
		{
			var chara:Character = state as Character;
			if(chara == null)
			{
				return StateTypeEnum.NONE;
			}
			var oper:int = node.arguments[0]
			var val:int = node.arguments[1];
			switch(oper)
			{
				case Operator.Equals:
				{
					return chara.state == val;
					break;
				}
				case Operator.NotEquals:
				{
					return chara.state != val;
					break;
				}
			}
		}
		
		public static function parse(state:ParseState):Node
		{
			var operator:int = state.currentOperator;
			if(operator != Operator.Equals && operator != Operator.NotEquals)
			{
				return null;
			}
			state.tokenIndex++;
			
			var text:String = state.currentToken.text;
			var stateType:int = getStateType(text);
			
			if(stateType == StateTypeEnum.Unchanged || stateType == StateTypeEnum.NONE)
			{
				return null;
			}
			state.tokenIndex++;
			
			state.baseNode.arguments.push(operator);
			state.baseNode.arguments.push(stateType);
			
			return state.baseNode;
		}
		
		private static function getStateType(text:String):int
		{
			var rlt:int = StateTypeEnum.NONE;
			switch(text.toLocaleLowerCase())
			{
				case "s":
				{
					rlt = StateTypeEnum.Standing;
					break;
				}
				case "a":
				{
					rlt = StateTypeEnum.Airborne;
					break;
				}
				case "c":
				{
					rlt = StateTypeEnum.Crouching;
					break;
				}
				case "u":
				{
					rlt = StateTypeEnum.Unchanged;
					break;
				}
			}	
			return rlt;
		}
	}
}